<?
class Spaph_Request {
    private $isPost  = false; // Use _POST or _GET
    
    public function __construct()
    {
        $this->isPost( count($_POST) > count($_GET) );
    }

    /**
     * Return the post or get value of a request.
     *
     * @param string $key
     * @param string $val default value
     * @return string
     */
    public function get($key,$val='')
    {
        if( ! $this->exists($key) ) {
            return $val;
        }

        if($this->isPost()) {
            return $_POST[$key];
        }
        else {
            return $_GET[$key];
        }
    }
    
    /**
     * Return all of POST or GET
     *
     * @return array
     */
    public function getAll()
    {
        return $this->isPost() ? $_POST : $_GET;
    }
    
    /**
     * Return the html escaped post or get value of a request.
     *
     * @param string $key
     * @param string $val default value
     * @return string
     */
    public function getx($key=null,$val='')
    {
        return htmlspecialchars($this->get($key,$val));
    }
    
    /**
     * Return the uri escaped post or get value of a request.
     *
     * @param string $key
     * @param string $val default value
     * @return string
     */
    public function getxuri($key=null,$val='')
    {
        $string = $this->get($key,$val);
        return rawurlencode((binary)$string);
    }
    
    /**
     * Set a post or get value.
     *
     * @param string $key
     * @param string $val default value
     * @return string
     */
    public function set($key,$val='')
    {
        if($this->isPost()) {
            $_POST[$key] = $val;
        }
        else {
            $_GET[$key] = $val;
        }
        
        return $val;
    }
    
    /**
     * Check if a key exists.
     *
     * @param string $key
     * @return boolean
     */
    public function exists($key)
    {
        return ( isset($_POST[$key]) || isset($_GET[$key]) );
    }
    
    /**
     * Check the $val against get($key).
     *
     * @param string $key
     * @param string $val
     * @return boolean
     */
    public function isChecked($key, $val)
    {
        return $this->get($key) == $val;
    }

    /**
     * Set or get whether to use $_POST or $_GET
     *
     * @param boolean $boolean
     * @return boolean
     */    
    public function isPost($boolean=null)
    {
        if(isset($boolean)) {
            $this->isPost = $boolean;
        }
        
        return $this->isPost;
    }
    
    /**
     * Are $_POST and $_GET empty? Both get and post have at 
     * most one item, which could be the _action_param 'do'.
     * So, even with 'do', they are considered empty.
     *
     * @return boolean
     */    
    public function isEmpty()
    {
        $count = 0;
        
        if($this->isPost()) {
            $count = count(array_keys($_POST));
        }
        else {
            $count = count(array_keys($_GET));
        }
        
        // Take away the action param 'do'.
        if($this->exists(Spaph::getInstance()->getActionParam())) {
            $count = $count - 1;
        }
        
        return !$count;
    }
}
